package com.fucking.great.context.plun;

import cn.hutool.core.collection.CollectionUtil;
import com.fucking.great.context.util.GetMapDecorator;
import com.fucking.great.context.util.PrefixKeyMapDecorator;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public abstract class BasePlug implements Plug{
    private final Set<String> importSet;
    private final Map<String, String> config;
    private final Map<String, Object> userPlun = new HashMap<>();
    public BasePlug(Set<String> importSet, Map<String, String> config) {
        this.importSet = importSet;
        this.config = config;
        initUserPlunMap(this.userPlun);
    }

    public BasePlug(Map<String, String> config,String [] importStr) {
        this(CollectionUtil.newHashSet(importStr),config);
    }

    @Override
    public void commit() {

    }
    @Override
    public void quietRollback() {

    }

    @Override
    public Map<String, Object> beginUser() {
        return this.userPlun;
    }

    public Map<String, Object> getAllUserPlun() {
        return this.userPlun;
    }
    protected abstract void initUserPlunMap(Map<String, Object> userPlun);

    public Set<String> importStr() {
        return this.getImportSet();
    }
    public Set<String> getImportSet() {
        return importSet;
    }

    public GetMapDecorator<String, String> getConfig() {
        return new GetMapDecorator<String, String>(config);
    }

    /**
     * 使用key时候，默认添加前缀。 <br/>
     * @see PrefixKeyMapDecorator
     * @param prefix
     * @return
     */
    public GetMapDecorator<String, String> getPrefixConfig(String prefix) {
        return new GetMapDecorator<String, String>(new PrefixKeyMapDecorator<String, String>(prefix,config));
    }

    public <T> T  getUserPlun(String userPlunName){
      return (T) this.getAllUserPlun().get(userPlunName);
    }


}
